<?php

/**********************\

File     :	.php
Created  :	
Updated  :  
Author   :	
Function :	
Comments :	

\**********************/


	switch($task) {
		case 'remove':
		case 'delete':
			province_delete(  $option,  intval( $cid[0] ) );
		case 'orderup':
			province_order(  intval( $cid[0] ), -1, $option );
			break;
		case 'orderdown':
			province_order(  intval( $cid[0] ), 1, $option );
			break;
		case "search":
			break;
		case "new":
		case "edit":
			province_edit( $option,  intval( $cid[0] ));
			break;
		case "save":
			province_save( $option, $p_id );
			break;
		case "list":
		default:
			province_list( $option );
			break;
	}


function province_delete(  $option, $p_id ) {
	global $database, $access;

	$row = new congregationProvince( $database );
	$row->load( $p_id );

	if(!$row->p_id) {
		echo "<script> alert('". _Province_Not_Found ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "SELECT count(*) FROM #__congregation_casa WHERE p_id='$row->p_id'";
	$database->setQuery( $query );

	$case = $database->loadResult();

	if($case > 0 ) {
		echo "<script> alert('". _Province_Has_Houses ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "DELETE FROM #__congregation_province "
	. "\n WHERE p_id = '$p_id' "
	;
	$database->setQuery( $query );
	if (!$database->query()) {
		echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
	}
	mosRedirect( ("?option=$option&rub=province&Itemid=$Itemid"));
	
}



/**
* Moves the order of a record
* @param integer The increment to reorder by
*/
function province_order( $uid, $inc, $option ) {
	global $database, $option, $rub, $Itemid;

	$row = new congregationProvince( $database );
	
	$row->load( $uid );
	$row->id = $row->p_id;
	$row->move( $inc );

	mosRedirect( ("?option=$option&rub=$rub") );
}

function province_list( $option ) {
	global $database;

//for listing
	$limit 		= intval( mosGetParam( $_REQUEST, 'limit', 20 ) );
	$limitstart = intval( mosGetParam( $_REQUEST, 'limitstart', 0 ) );

	$query = "SELECT count(*) FROM #__congregation_province ";

	$database->setQuery( $query );
	$total = $database->loadResult();
	
	if ( $total <= $limit ) $limitstart = 0;

	require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );

	$pageNav = new mosPageNav( $total, $limitstart, $limit  );


	$query = "SELECT #__congregation_province.*, count(#__congregation_casa.c_id) AS p_case FROM #__congregation_province LEFT JOIN #__congregation_casa on #__congregation_province.p_id=#__congregation_casa.p_id GROUP BY #__congregation_province.p_id "
	. "\nORDER BY ordering "
	. "\nLIMIT ".$pageNav->limitstart.",".$pageNav->limit
	;
	

	
	$database->setQuery( $query );
	$rows =  $database->loadObjectList();

	HTML_province::province_list( $rows, $option, $pageNav );

}
function province_edit( $option, $p_id ) {
	global $access, $database;

	$row = new congregationProvince( $database );
	$row->load( $p_id );
	
	if(!$p_id) {
		$row->ordering = 0;
	}
	$lists = array();

	// build the html select list for ordering
	$query = "SELECT ordering AS value, p_name AS text"
	. "\n FROM #__congregation_province"
	. "\n ORDER BY ordering "
	;
	$lists['ordering'] 		= mosAdminMenus::SpecificOrdering( $row, $p_id, $query, 1 );

  HTML_province::province_edit( $row, $option, $lists );
}

function province_save( $option, $p_id ) {
	global $Itemid, $rub, $database;

	$row = new congregationProvince( $database );
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	if (!$row->check()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}

	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	$row->updateOrder();
	mosRedirect( "?option=$option&rub=$rub&Itemid=$Itemid", "Saved" );


}

class HTML_province {
	
//Province
	function province_edit( &$row, $option, &$lists ) {
		global $params, $Itemid;
			?>
		<table class="adminheading">
		<tr>
			<th>
			<?php echo _Province_Edit; ?>
			</th>
		</tr>
		</table>
			<form method="post" action="" name="adminForm">
					<input type="hidden" name="p_id" value="<?php echo $row->p_id ;?>" />
					<input type="hidden" name="option" value="<?php echo $option ;?>" />
					<input type="hidden" name="rub" value="province" />
					<input type="hidden" name="task" value="" />
			<table  class="adminform">
				<tr valign="top">
					<td><?php echo _Province_Name ; ?> : </td>
					<td><input type="text" name="p_name"  style="width: 300px" value="<?php echo $row->p_name ; ?>"/></td>
				</tr>
			<tr>
				<td align="left">
				<?php echo _E_ORDERING; ?>
				</td>
				<td>
				<?php echo $lists['ordering']; ?>
				</td>
			</tr>
			</table>
			</form>
	<?php

	}


	function province_list( &$rows, $option, &$pageNav) {
		global $params, $Itemid, $access;
			?>
		<table class="adminheading">
		<tr>
			<th>
			<?php echo _Province_List; ?>
			</th>
		</tr>
		</table>
		<form action="index2.php" method="post" name="adminForm">
		<table class="adminlist">
				<th width="20">
				#
				</th>
				<th width="20">
				<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $rows ); ?>);" />
				</th>
					<th><?php echo _Province_Name ; ?> </th>
				<th colspan="2" width="5%">
				<?php echo _E_ORDERING; ?>
				</th>
					<th><?php echo _Province_Communities ; ?></th>
				</tr>
				<?php
				if($rows) {
				$k = 0;
				for ($i=0, $n=count( $rows ); $i < $n; $i++) {
					$row = &$rows[$i];
					$k = 1 - $k;
					
					$row->id = $row->p_id;
					$checked 	= mosCommonHTML::CheckedOutProcessing( $row, $i );

					$link 		= 'index2.php?option=com_congregation&rub=province&task=edit&hidemainmenu=1&id='. $row->p_id;
						?>
			<tr class="<?php echo "row$k"; ?>">
				<td  width="20">
				<?php echo $pageNav->rowNumber( $i ); ?>
				</td>
				<td  width="20">
				<?php echo $checked; ?>
				</td>
					<td><?php echo $row->p_name ; ?> </td>
				<td align="center">
				<?php echo $pageNav->orderUpIcon( $i ); ?>
				</td>
				<td align="center">
				<?php echo $pageNav->orderDownIcon( $i, $n ); ?>
				</td>
					<td align="center"><?php echo $row->p_case . '<a href="' . ( "index2.php?option=$option&rub=casa&Itemid=$Itemid&p_id=" . $row->p_id) . '">' .  ' (' . _Congregation_View . ')</a>'; ?></td>

				
				</tr>
						<?php
					}
				}
				?>
			</table>
		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="rub" value="province" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="hidemainmenu" value="0" />
		<?php
		echo $pageNav->getListFooter(); 
		?>
			</form>
<?php
	
	
	}


}
?>
